package cn.unknowpupil.init.interceptor;


import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.utils.strings.MapperUtils;
import cn.unknowpupil.init.utils.security.ServletUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created at 2020/10/20 by w10g <br>
 * Initialize AccessDeniedHandlerImpl ... <br>
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Component
public class AccessDeniedHandlerImpl  implements AccessDeniedHandler , Serializable {


    private static final long serialVersionUID = 3428734500698860226L;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        int code = HttpStatus.FORBIDDEN.value();
        String path = request.getRequestURI();
        ServletUtils.renderString(response, MapperUtils.obj2json(ResultResponse.buildFailure(code, e.getMessage(),path)));

    }
}
